import java.sql.*;
import java.util.Scanner;

public class Main {
    public static void main(String[] args){
        //查询
      //  m1();
        //条件查询
       // m2();
        //增加
       //m3();
        //删除
        //m4();
        //修改
        m5();
//抽取相同方法
        //.var 能帮忙生成变量，变量名
        //为了避免运行时出错，写入try...catch



    }

    private static void m5() {
        //1.加载驱动
        try {
            //Scanner 实现键盘录入
//            Scanner sc=new Scanner(System.in);
//            System.out.println("请输入你要修改对象的id：");
//            int id=sc.nextInt();
//            System.out.println("请说明修改后的姓名:");
//            String name = sc.next();
//            System.out.println("请说明修改后的密码:");
//            String pass = sc.next();
            Class.forName("com.mysql.cj.jdbc.Driver");

            //2.获取连接(url username password)
            String url = "jdbc:mysql://127.0.0.1:3306/jdbc?characterEncoding=utf-8";
            Connection connection = DriverManager.getConnection(url, "root", "20050324");
            //3.写sql
            String sql="update sys_user set name=?,pass=? where id = ?";
            //4.预编译sql
            PreparedStatement preparedStatement=connection.prepareStatement(sql);
            //5.有参数设置参数
//            preparedStatement.setObject(1,name);//setObject能包括所有类型值
//            preparedStatement.setObject(2,pass);
//            preparedStatement.setObject(3,id);
            preparedStatement.setObject(1,"zhangtao");//setObject能包括所有类型值
            preparedStatement.setObject(2,777);
            preparedStatement.setObject(3,5);
            //6.执行sql executeUpdate增删改，返回int类型，影响int行结果，int值大于0，说明影响成功
            int result=preparedStatement.executeUpdate();
            //7.结果判断
            if(result>0){
                System.out.println("修改成功");
            }
            //8.关闭资源
            preparedStatement.close();
            connection.close();
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }catch (SQLException e){
            throw new RuntimeException(e);
        }
    }

    private static void m4() {
        //1.加载驱动
        try {
            //Scanner 实现键盘录入
//            Scanner sc=new Scanner(System.in);
//            System.out.println("请说明你要新增的姓名:");
//            String name = sc.next();
//            System.out.println("请说明你要新增的密码:");
//            String pass = sc.next();

            Class.forName("com.mysql.cj.jdbc.Driver");
            //2.获取连接(url username password)
            String url = "jdbc:mysql://127.0.0.1:3306/jdbc?characterEncoding=utf-8";
            Connection connection = DriverManager.getConnection(url, "root", "20050324");
            //3.写sql
            String sql="delete from sys_user where id =?";
            //4.预编译sql
            PreparedStatement preparedStatement=connection.prepareStatement(sql);
            //5.有参数设置参数
            preparedStatement.setObject(1,"3");
            //6.执行sql executeUpdate
            int result=preparedStatement.executeUpdate();
            //7.结果判断
            if(result>0){
                System.out.println("删除成功");
            }
            //8.关闭资源
            preparedStatement.close();
            connection.close();
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }catch (SQLException e){
            throw new RuntimeException(e);
        }
    }

    private static void m3() {
        //1.加载驱动
        try {
            //Scanner 实现键盘录入
            Scanner sc=new Scanner(System.in);
            System.out.println("请说明你要新增的姓名:");
            String name = sc.next();
            System.out.println("请说明你要新增的密码:");
            String pass = sc.next();

            Class.forName("com.mysql.cj.jdbc.Driver");
            //2.获取连接(url username password)
            String url = "jdbc:mysql://127.0.0.1:3306/jdbc?characterEncoding=utf-8";
            Connection connection = DriverManager.getConnection(url, "root", "20050324");
            //3.写sql
            String sql="insert into sys_user(name,pass) values (?,?)";
            //4.预编译sql
            PreparedStatement preparedStatement=connection.prepareStatement(sql);
            //5.有参数设置参数
            preparedStatement.setObject(1,name);
            preparedStatement.setObject(2,pass);
            //6.执行sql executeUpdate
          int result=preparedStatement.executeUpdate();
            //7.结果判断
            if(result>0){
                System.out.println("新增成功");
            }
            //8.关闭资源
            preparedStatement.close();
            connection.close();
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }catch (SQLException e){
            throw new RuntimeException(e);
        }
    }

    private static void m2() {
        //1.加载驱动
        try {
            //Scanner 实现键盘录入
            Scanner sc=new Scanner(System.in);
            System.out.println("请说明你要查询的姓名:");
            String word = sc.next();

            Class.forName("com.mysql.cj.jdbc.Driver");
            //2.获取连接(url username password)
            String url = "jdbc:mysql://127.0.0.1:3306/jdbc?characterEncoding=utf-8";
            Connection connection = DriverManager.getConnection(url, "root", "20050324");
            //3.写sql
            String sql="select * from sys_user where name like ?";
            //4.预编译sql
            PreparedStatement preparedStatement=connection.prepareStatement(sql);
            //5.有参数设置参数
            preparedStatement.setObject(1,"%"+word+"%");
            //6.执行sql
            ResultSet resultSet = preparedStatement.executeQuery();
            //7.遍历结果集
            while (resultSet.next()){
                int id = resultSet.getInt(1);
                String name = resultSet.getString(2);
                String pass = resultSet.getString(3);
                System.out.println(id+"---"+name+"---"+pass);
            }
            //8.关闭资源
            resultSet.close();
            preparedStatement.close();
            connection.close();
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }catch (SQLException e){
            throw new RuntimeException(e);
        }
    }

    private static void m1() {
        //1.加载驱动
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            //2.获取连接(url username password)
            String url = "jdbc:mysql://127.0.0.1:3306/jdbc?characterEncoding=utf-8";
            Connection connection = DriverManager.getConnection(url, "root", "20050324");
//           if(connection!=null){
//               System.out.println("连接成功");
//           }测试成功后注释掉
            //3.写sql
            String sql="select * from sys_user";
            //4.预编译sql
            PreparedStatement preparedStatement=connection.prepareStatement(sql);
            //5.有参数设置参数
            //6.执行sql
            ResultSet resultSet = preparedStatement.executeQuery();
            //7.遍历结果集
            while (resultSet.next()){
                int id = resultSet.getInt(1);
                String name = resultSet.getString(2);
                String pass = resultSet.getString(3);
                System.out.println(id+"---"+name+"---"+pass);
            }
            //8.关闭资源
            resultSet.close();//修改操作会多这一步
            preparedStatement.close();
            connection.close();
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }catch (SQLException e){
            throw new RuntimeException(e);
        }
    }
}